package com.hellobike.haxing.modules.wms.service.impl;

import com.hellobike.haxing.modules.wms.entity.Imperfections;
import com.hellobike.haxing.modules.wms.entity.ReturnSupplierDetail;
import com.hellobike.haxing.modules.wms.enums.ImperfectionStatusEnum;
import com.hellobike.haxing.modules.wms.enums.ReturnStatusEnum;
import com.hellobike.haxing.modules.wms.enums.ReturnTypeEnum;
import com.hellobike.haxing.modules.wms.enums.SkuReturnStatusEnum;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.mapper.ImperfectionsMapper;
import com.hellobike.haxing.modules.wms.service.IImperfectionsService;
import com.hellobike.haxing.modules.wms.service.IReturnSupplierDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: t_wms_imperfections
 * @Author: haxing-boot
 * @Date:   2020-02-19
 * @Version: V1.0
 */
@Service
public class ImperfectionsServiceImpl extends ServiceImpl<ImperfectionsMapper, Imperfections> implements IImperfectionsService {

    @Autowired
    private IReturnSupplierDetailService returnSupplierDetailService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void audit(Long id, String imperfectionStatus, Long warehouseId, Integer sysUserId) throws WmsServiceException {
        Imperfections imperfections = getById(id);
        if (imperfections == null) {
            throw new WmsServiceException("残次处理不存在");
        }
        if (!ImperfectionStatusEnum.INIT.getValue().equals(imperfections.getImperfectionStatus())) {
            throw new WmsServiceException("已处理该残次记录，不能重复处理");
        }
        imperfections.setImperfectionStatus(imperfectionStatus);
        updateById(imperfections);
        if(ImperfectionStatusEnum.RS.getValue().equals(imperfectionStatus)){
            ReturnSupplierDetail returnSupplierDetail = new ReturnSupplierDetail();
            returnSupplierDetail.setSkuId(imperfections.getSkuId());
            returnSupplierDetail.setSkuCode(imperfections.getSkuCode());
            returnSupplierDetail.setBarcode(imperfections.getBarcode());
            returnSupplierDetail.setReturnQty(imperfections.getQty());
            returnSupplierDetail.setReturnReason(imperfections.getImperfectionReason());
            returnSupplierDetail.setReturnTime(imperfections.getApplyTime());
            returnSupplierDetail.setReturnType(ReturnTypeEnum.SKU.getValue());
            returnSupplierDetail.setReturnStatus(ReturnStatusEnum.INIT.getValue());
            returnSupplierDetail.setWarehouseId(imperfections.getWarehouseId());
            returnSupplierDetail.setSupplierId(imperfections.getSupplierId());
            returnSupplierDetail.setSkuStatus(imperfections.getSkuStatus());
            returnSupplierDetail.setShopId(-1L);
            returnSupplierDetailService.save(returnSupplierDetail);
        }
    }
}
